package com.woniuxy.escort.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.entity.Escort;
import com.woniuxy.commons.service.FeignUserService;
import com.woniuxy.commons.util.DateUtil;
import com.woniuxy.commons.util.MD5Util;
import com.woniuxy.commons.util.PageMessage;
import com.woniuxy.escort.mapper.EscortMapper;
import com.woniuxy.escort.service.EscortService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Service
public class EscortServiceImpl implements EscortService {
@Resource
private EscortMapper escortMapper;
@Resource
private FeignUserService feignUserService;
    @Override
    public PageMessage<List<Escort>> findAllEscort(int page, int size) {
        PageHelper.startPage(page,size);
        List<Escort> escorts = escortMapper.findAllEscort();
        PageInfo<Escort> pageInfo= PageInfo.of(escorts);
        // 封装信息
        PageMessage<List<Escort>> pageMessage = new PageMessage<>();
        pageMessage.setCurrentPage(page);
        pageMessage.setSize(size);
        pageMessage.setTotalPage(pageInfo.getPages());
        pageMessage.setTotal(pageInfo.getTotal());
        pageMessage.setData(escorts);
        return pageMessage;
    }

    @Override
    @Transactional
    @Caching(evict = {
            @CacheEvict(value = "findAllEscort", allEntries = true)
    })
    public boolean addEscort(Escort escort) {
         escort.setCreateTime(DateUtil.getDate());
        return escortMapper.addEscort(escort);
    }

    @Override
    @Transactional
    @Caching(evict = {
            @CacheEvict(value = "findAllEscort", allEntries = true),
            @CacheEvict(value = "findEscortByUid", key = "{'escort-'+#uid}")
    })
    public boolean delEscortByUid(int uid) {

        return escortMapper.delEscortByUid(uid);
    }

    @Override
    @Cacheable(value = "findEscortByUid",key = "#uid")
    public Escort findEscortByUid(int uid) {

        return escortMapper.findEscortByUid(uid);
    }

    @Override
    @Transactional
    @CacheEvict(value = "findAllEscort", allEntries = true)
    public boolean updateEscort(Escort escort) {

        return escortMapper.updateEscort(escort);
    }

    @Override
    public PageMessage<List<Escort>> findEscortByConditions(Escort escort, int page, int size) {
        PageHelper.startPage(page, size);
        List<Escort> escorts = escortMapper.findEscortByConditions(escort);
        PageInfo<Escort> pageInfo = PageInfo.of(escorts);
        // 封装信息
        PageMessage<List<Escort>> pageMessage = new PageMessage<>();
        pageMessage.setCurrentPage(page);
        pageMessage.setSize(size);
        pageMessage.setTotalPage(pageInfo.getPages());
        pageMessage.setTotal(pageInfo.getTotal());
        pageMessage.setData(escorts);
        return pageMessage;
    }

    @Override
    @Transactional
    @CacheEvict(value = "findAllEscort", allEntries = true)
    public boolean resetPassword(int uid) {
//将密码重置为123456但需要加密
        String password = "123456";
        try {
            password = MD5Util.md5(password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return escortMapper.resetPassword(uid,password);
    }

    @Override
    public List<Escort> isExist(Escort escort) {
        return escortMapper.isExist(escort);
    }
}
